package com.base.cn.platform.os.controller.planTools;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.service.manage.planTools.PopupSloganService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 弹窗Controller
 *
 * @author lxr
 * @create 2020-06-29-17:46
 */
@Controller
@Slf4j
@RequestMapping("/admin/planTools/popupSlogan")
public class AdminPopupSloganController extends AdminBaseController {

    @Autowired
    private PopupSloganService popupSloganService;

    /**
     * 初始化弹窗广告列表
     *
     * @return 列表页面
     */
    @RequestMapping("/queryPopupSloganList")
    public String queryPopupSloganList() {
        return resultPage("/planTools/popupSlogan/popupSlogan-list");
    }

    /**
     * 加载弹窗广告数据
     *
     * @param model
     * @param params      查询条件
     * @param currentPage 页码
     * @return 显示页面
     */
    @RequestMapping("/ajaxQueryPopupSloganList")
    public String ajaxQueryPopupSloganList(Model model,
                                           @RequestBody Map<String, Object> params,
                                           @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage) {
        Map<String, Object> dataMap = popupSloganService.findPopupSloganListPage(params,currentPage);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/planTools/popupSlogan/ajax-popupSlogan-list");
    }

    /**
     * 创建弹窗广告
     */
    @RequestMapping(value = {"/initUpdate", "/initAdd"})
    public String initPopupSlogan(Model model,
                                  @RequestParam(value = "id", required = false, defaultValue = "0") BigDecimal id) {
        Map<String, Object> popupSlogan = null;
        if (DataUtil.idIsNotNull(id)) {
            popupSlogan = popupSloganService.findPopupSloganById(id);
        }
        if (popupSlogan == null) {
            popupSlogan = new HashMap<>();
        }
        model.addAttribute("popupSlogan", popupSlogan);
        return resultPage("/planTools/popupSlogan/save-popupSlogan");
    }

    /**
     * 创建弹窗广告
     */
    @RequestMapping("/savePopupSlogan")
    @ResponseBody
    public Map<String, Object> savePopupSlogan(HttpServletRequest request,
                                                 @RequestParam Map<String, Object> popupSlogan) {
        popupSlogan.put("createUserId", loginUtil.getLoginUserId(request));
        return popupSloganService.savePopupSlogan(popupSlogan);
    }

    /**
     * 修改状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("/updatePopupSloganStatus")
    @ResponseBody
    public Map<String,Object> updatePopupSloganStatus(@RequestParam("id") String id,@RequestParam("status") int status){
        return popupSloganService.updatePopupSloganStatus(id,status);
    }

}
